package com.qzsoft.zhjd.asphalt.modules.videomonitor.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.qzsoft.system.modules.sysstation.entity.SysStationEntity;
import com.qzsoft.system.modules.sysstation.enums.SysStationType;
import com.qzsoft.system.modules.sysstation.service.SysStationService;
import com.qzsoft.system.modules.zhjdsysdept.entity.ZhjdSysDeptEntity;
import com.qzsoft.system.modules.zhjdsysdept.service.ZhjdSysDeptService;
import com.qzsoft.zhjd.asphalt.modules.videomonitor.service.AsphaltVideoMonitorService;
import com.qzsoft.zhjd.monitoring.modules.videomonitor.dto.VideoMonitorTreeDto;

/**
 * @ClassName AsphaltVideoMonitorServiceImpl
 * @Description 沥青视频监控业务接口实现
 * @author linhuawei
 * @since 2022-03-22
 */
@Service
public class AsphaltVideoMonitorServiceImpl implements AsphaltVideoMonitorService {

    @Autowired
    private ZhjdSysDeptService zhjdSysDeptService;

    @Autowired
    private SysStationService sysStationService;

    @Override
    public List<VideoMonitorTreeDto> tree() {
        // 获取基地列表
        Map<String, Object> params = new HashMap<String, Object>();
        List<ZhjdSysDeptEntity> baseList = zhjdSysDeptService.listForBase(params);
        List<VideoMonitorTreeDto> result = new ArrayList<VideoMonitorTreeDto>();
        if (null != baseList && 0 < baseList.size()) {
            // 拼接基地列表
            Map<String, VideoMonitorTreeDto> map = new LinkedHashMap<String, VideoMonitorTreeDto>();
            VideoMonitorTreeDto dto;
            for (ZhjdSysDeptEntity entity : baseList) {
                dto = new VideoMonitorTreeDto();
                dto.setId(entity.getCode());
                dto.setLabel(entity.getName());
                dto.setType("base");
                dto.setChildren(new ArrayList<VideoMonitorTreeDto>());
                map.put(entity.getCode(), dto);
            }

            // 获取拌合楼列表
            params.clear();
            params.put("type", SysStationType.ASPHALT.getValue());
            List<SysStationEntity> stationList = sysStationService.list(params);
            VideoMonitorTreeDto parent;
            for (SysStationEntity entity : stationList) {
                parent = map.get(entity.getBaseCode());
                if (null != parent) {
                    dto = new VideoMonitorTreeDto();
                    dto.setId(entity.getCode());
                    dto.setLabel(entity.getName());
                    dto.setType("station");
                    parent.getChildren().add(dto);
                }
            }

            // 转化数据
            map.forEach((key, value) -> {
                result.add(value);
            });
        }

        return result;
    }

}
